﻿namespace WLFramework.Log
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Reflection;
    using System.Web;

    /// <summary>
    /// 类，事件日志类。
    /// 单态封装log4net
    /// </summary>
    public static class Logger
    {
        public static bool IsConfigurated { get; internal set; }
        public static string LogName { get; private set; }

        private static log4net.ILog log = null;
        private static object lockHelper = new object();

        /// <summary>
        /// 初始化日志类
        /// </summary>
        static Logger()
        {
            LogName = Assembly.GetExecutingAssembly().FullName;
            InitializeConfiguration();
        }

        /// <summary>
        /// 返回一个ILog的唯一实例， 双检锁,线程安全。
        /// </summary>
        public static log4net.ILog Log
        {
            get
            {
                if (log == null)
                {
                    lock (lockHelper)
                    {
                        if (log == null)
                        {
                            log = log4net.LogManager.GetLogger(LogName);
                        }
                    }
                }

                return log;
            }
        }

        /// <summary>
        /// 记录DEBUG信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="ex"></param>
        public static void Debug(string title, Exception ex = null)
        {
            if (ex != null)
            {
                Log.Debug(title, ex);
            }
            else
            {
                Log.Debug(title);
            }
        }

        /// <summary>
        /// 记录DEBUG信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void DebugFormat(string format, params object[] args)
        {
            Log.DebugFormat(CultureInfo.CurrentCulture, format, args);
        }
        
        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="ex"></param>
        public static void Error(string title, Exception ex = null)
        {
            if (ex != null)
            {
                Log.Error(title, ex);
            }
            else
            {
                Log.Error(title);
            }
        }

        /// <summary>
        /// 记录错误信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void ErrorFormat(string format, params object[] args)
        {
            Log.ErrorFormat(CultureInfo.CurrentCulture, format, args);
        }
        
        /// <summary>
        /// 记录严重错误信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="ex"></param>
        public static void Fatal(string title, Exception ex = null)
        {
            if (ex != null)
            {
                Log.Fatal(title, ex);
            }
            else
            {
                Log.Fatal(title);
            }
        }

        /// <summary>
        /// 记录严重错误信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void FatalFormat(string format, params object[] args)
        {
            Log.FatalFormat(CultureInfo.CurrentCulture, format, args);
        }

        /// <summary>
        /// 记录普通信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="ex"></param>
        public static void Info(string title, Exception ex = null)
        {
            if (ex != null)
            {
                Log.Info(title, ex);
            }
            else
            {
                Log.Info(title);
            }
        }

        /// <summary>
        /// 记录普通信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void InfoFormat(string format, params object[] args)
        {
            Log.InfoFormat(CultureInfo.CurrentCulture, format, args);
        }
        
        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="ex"></param>
        public static void Warn(string title, Exception ex = null)
        {
            if (ex != null)
            {
                Log.Warn(title, ex);
            }
            else
            {
                Log.Warn(title);
            }
        }

        /// <summary>
        /// 记录警告信息
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void WarnFormat(string format, params object[] args)
        {
            Log.WarnFormat(CultureInfo.CurrentCulture, format, args);
        }
        
        private static void InitializeConfiguration()
        {
            try
            {
                log4net.Config.XmlConfigurator.Configure();
                IsConfigurated = true;
            }
            catch (Exception /*ex*/)
            {

            }
        }
    }
}